#!/bin/bash

### get and set parameter
# ubuntu16.04/x64
PLATFORM=$(uname -m)
if [ $PLATFORM == "x86_64" ];then
    RUN_PLATFORM=xenial_amd64
else
    RUN_PLATFORM=arm64
fi

echo $RUN_PLATFORM

ARCHITECTURE=all
# set devel/install
INSTALL_TYPE=devel
# get shell absolute path, make sure shell always in package root dir
PRJ_ROOT_PATH=$(cd $(dirname $0); pwd)
# get app name from package.xml and trim left/right blank space
PRJ_NAME=$(xmllint --xpath "/package/name/text()" $PRJ_ROOT_PATH/package.xml | sed 's/^ *\| *$//g')
# deb name could not include '_'!!! => aaa_bbb_ccc -> aaa-bbb-ccc
PRJ_NAME_DEB=${PRJ_NAME//'_'/'-'}
# get app version from package.xml and trim left/right blank space
PRJ_VERSION=$(xmllint --xpath "/package/version/text()" $PRJ_ROOT_PATH/package.xml | sed 's/^ *\| *$//g')
# get app description from package.xml and trim left/right blank space
PRJ_DESC=$(xmllint --xpath "/package/description/text()" $PRJ_ROOT_PATH/package.xml | sed 's/^ *\| *$//g')
# search catkin workspace src/ folder, if found, then it is the catkin workspace root dir
cd $PRJ_ROOT_PATH
while true
do
    cd ..
    cur_dir=$(pwd)
    ls src/ > /dev/null 2>&1

    if [ $? != 0 ];then
        # if reach / dir, exit
        if [ $cur_dir == "/" ];then
            echo "Err: Can't find catkin workspace root!"
            exit 1
        else
            continue
        fi
    else
        break
    fi
done
CATKIN_ROOT_PATH=$cur_dir

function version_check(){
    if [ $1 != ${PRJ_VERSION} ];then
        echo "Err: Version check fail!"
        exit 1
    fi
}

function ros_catkin_make(){
    echo $CATKIN_ROOT_PATH

    ### catkin make
    cd $CATKIN_ROOT_PATH
    . /opt/ros/kinetic/setup.bash
    catkin_make -DCATKIN_WHITELIST_PACKAGES=$PRJ_NAME clean
    catkin_make -DCATKIN_WHITELIST_PACKAGES=$PRJ_NAME

    if [ $? != 0 ];then
        echo "Err: Catkin make fail!"
        exit 1
    fi
}

function prepare_files(){
    cd $PRJ_ROOT_PATH
    # create dir
    mkdir -p deb_tmp/DEBIAN

    ## copy generated ros file
    # common need to copy
    if [ -e $CATKIN_ROOT_PATH/${INSTALL_TYPE}/lib/pkgconfig/${PRJ_NAME}.pc ];then
        mkdir -p deb_tmp/opt/ros/kinetic/lib/pkgconfig
        cp $CATKIN_ROOT_PATH/${INSTALL_TYPE}/lib/pkgconfig/${PRJ_NAME}.pc deb_tmp/opt/ros/kinetic/lib/pkgconfig

        ## modify
        # .pc file => /my_catkin_work_dir/devel/ -> /opt/ros/kinetic/
        sed -i "s:${CATKIN_ROOT_PATH}/${INSTALL_TYPE}:/opt/ros/kinetic:g" deb_tmp/opt/ros/kinetic/lib/pkgconfig/${PRJ_NAME}.pc
    fi

    if [ -d $CATKIN_ROOT_PATH/${INSTALL_TYPE}/share/${PRJ_NAME}/ ];then
        mkdir -p deb_tmp/opt/ros/kinetic/share/
        cp -r $CATKIN_ROOT_PATH/${INSTALL_TYPE}/share/${PRJ_NAME}/ deb_tmp/opt/ros/kinetic/share/

        # modify cmake files
        # /my_catkin_work_dir/devel/ -> /opt/ros/kinetic/
        # /my_project_dir/ -> /opt/ros/kinetic/share/my_prj_name/
        cmake_files=$(ls deb_tmp/opt/ros/kinetic/share/${PRJ_NAME}/cmake)
        for cmake_file in $cmake_files;do
            sed -i "s:${PRJ_ROOT_PATH}:/opt/ros/kinetic/share/${PRJ_NAME}:g" deb_tmp/opt/ros/kinetic/share/${PRJ_NAME}/cmake/$cmake_file
            sed -i "s:${CATKIN_ROOT_PATH}/${INSTALL_TYPE}:/opt/ros/kinetic:g" deb_tmp/opt/ros/kinetic/share/${PRJ_NAME}/cmake/$cmake_file
        done
    fi

    # if message generated
    if [ -d $CATKIN_ROOT_PATH/${INSTALL_TYPE}/include/${PRJ_NAME} ];then
        mkdir -p deb_tmp/opt/ros/kinetic/include/
        cp -r $CATKIN_ROOT_PATH/${INSTALL_TYPE}/include/${PRJ_NAME}/ deb_tmp/opt/ros/kinetic/include/
    fi

    if [ -d $CATKIN_ROOT_PATH/${INSTALL_TYPE}/lib/python2.7/dist-packages/${PRJ_NAME} ];then
        mkdir -p deb_tmp/opt/ros/kinetic/lib/python2.7/dist-packages/
        cp -r $CATKIN_ROOT_PATH/${INSTALL_TYPE}/lib/python2.7/dist-packages/${PRJ_NAME}/ deb_tmp/opt/ros/kinetic/lib/python2.7/dist-packages/
    fi

    if [ -d $CATKIN_ROOT_PATH/${INSTALL_TYPE}/share/common-lisp/ros/${PRJ_NAME} ];then
        mkdir -p deb_tmp/opt/ros/kinetic/share/common-lisp/ros/
        cp -r $CATKIN_ROOT_PATH/${INSTALL_TYPE}/share/common-lisp/ros/${PRJ_NAME}/ deb_tmp/opt/ros/kinetic/share/common-lisp/ros/
    fi

    if [ -d $CATKIN_ROOT_PATH/${INSTALL_TYPE}/share/gennodejs/ros/${PRJ_NAME} ];then
        mkdir -p deb_tmp/opt/ros/kinetic/share/gennodejs/ros/
        cp -r $CATKIN_ROOT_PATH/${INSTALL_TYPE}/share/gennodejs/ros/${PRJ_NAME}/ deb_tmp/opt/ros/kinetic/share/gennodejs/ros/
    fi

    if [ -d $CATKIN_ROOT_PATH/${INSTALL_TYPE}/share/roseus/ros/${PRJ_NAME} ];then
        mkdir -p deb_tmp/opt/ros/kinetic/share/roseus/ros/
        cp -r $CATKIN_ROOT_PATH/${INSTALL_TYPE}/share/roseus/ros/${PRJ_NAME}/ deb_tmp/opt/ros/kinetic/share/roseus/ros/
    fi

    # if excutable generated
    if [ -d $CATKIN_ROOT_PATH/${INSTALL_TYPE}/lib/${PRJ_NAME}/ ];then
        mkdir -p deb_tmp/opt/ros/kinetic/lib/
        cp -r $CATKIN_ROOT_PATH/${INSTALL_TYPE}/lib/${PRJ_NAME}/ deb_tmp/opt/ros/kinetic/lib/ 2> /dev/null
    fi

    ## copy local files
    mkdir -p deb_tmp/opt/ros/kinetic/share/${PRJ_NAME}/
    cp *.xml deb_tmp/opt/ros/kinetic/share/${PRJ_NAME}/
    # copy folders [msg/launch/config/cfg/scripts/templates]
    local_folders=(msg launch config action service params scripts templates cfg data rviz rviz_cfg include)
    for folder in "${local_folders[@]}"
    do
        cp -r $folder deb_tmp/opt/ros/kinetic/share/${PRJ_NAME}/ 2> /dev/null
    done

    # modify debian control file
    cp -r debian/* deb_tmp/DEBIAN/
    DEB_SIZE=$(du -s -b deb_tmp/ | awk {'print $1'})
    
    sed -i "s/__PRJ_NAME__/ros-kinetic-leador-${PRJ_NAME_DEB}/g" deb_tmp/DEBIAN/control
    sed -i "s/__PRJ_VERSION__/${PRJ_VERSION}/g" deb_tmp/DEBIAN/control
    sed -i "s/__DEB_SIZE__/${DEB_SIZE}/g" deb_tmp/DEBIAN/control
    sed -i "s/__PRJ_DESC__/${PRJ_DESC}/g" deb_tmp/DEBIAN/control

    # some package has extra file to copy, becareful
    if [ -e copy_extra.sh ];then
        . copy_extra.sh
    fi
}

function release_deb(){
    # make deb
    fakeroot dpkg-deb -b deb_tmp ros-kinetic-leador-${PRJ_NAME_DEB}-${PRJ_VERSION}_${RUN_PLATFORM}.deb
    mkdir -p $CATKIN_ROOT_PATH/build_out/debs/
    mv *.deb $CATKIN_ROOT_PATH/build_out/debs/

    # clean
    rm -rf deb_tmp
}

# release source code
function release_src(){
    cd $PRJ_ROOT_PATH
    tar zcf ${PRJ_NAME}_${PRJ_VERSION}.tar.gz *
    mkdir -p $CATKIN_ROOT_PATH/build_out/pkg_release/
    mv ${PRJ_NAME}_${PRJ_VERSION}.tar.gz $CATKIN_ROOT_PATH/build_out/pkg_release/
}

### do action here
version_check $1
ros_catkin_make
prepare_files
release_deb
release_src

exit 0
